package m202405.d13;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/13 15:23
 */
public class Q102 {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            Queue<TreeNode> tempQueue = new LinkedList<>();
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    tempQueue.offer(node.left);
                }
                if (node.right != null) {
                    tempQueue.offer(node.right);
                }
            }
            queue = tempQueue;
            res.add(list);

        }
        return res;
    }
}
